package com.isoft.demo01.controller;


import com.isoft.demo01.bean.ResponseData;
import com.isoft.demo01.bean.ResultCode;
import com.isoft.demo01.dao.UserDao;
import com.isoft.demo01.entity.User;
import com.isoft.demo01.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user/admin")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserDao userDao;
    @PostMapping("/login")
    public ResponseData login(String username, String password){
        ResponseData req = new ResponseData();

        Map<String,Object> map = userService.getUser(username, password);
        String msg = "登录成功！" ;
        switch ((Integer)map.get("result")) {
            case 1 : msg = "账号不存在" ; break;
            case 2 : msg = "账号或者密码错误" ;break;
        }
        req.setErrorCode((Integer) map.get("result"));
        req.setMsg(msg);
        req.setData(map.get("data"));
        return  req ;
    }

    @PostMapping("/add")
    public ResponseData adduser(String username, String password,String email, String phone){
        ResponseData req=new ResponseData();
        boolean r = userService.adduser(username,password,email,phone);
        if (r){
            req.setErrorCode(ResultCode.SUCCESS.getCode());
            req.setMsg(ResultCode.SUCCESS.getMessage());
        }else {
            req.setMsg(ResultCode.COMMON_FAIL.getMessage());
            req.setErrorCode(ResultCode.COMMON_FAIL.getCode());
        }
            req.setData(r);
            return req;
    }

}
